<template>
	<view class="pagebox pd30 hidden">
		<view class="item" v-for="(v,i) in obj.list" :key="i">
			<view class="timelabel">{{v.time}}</view>
			<view class="card">
				<view class="miniitem flex-row" v-for="(k,n) in v.arr" :key="n" @click="go(k)">
					<view class="q flex1 mr20">{{k.messages[0].content}}</view>
					<text class="time">{{k.intime.slice(11,20)}}</text>
				</view>
			</view>
		</view>
		<view v-if="obj.list.length" class="w100 flex-center mt20 pb20"><uni-load-more :status="obj.loadstatus" :content-text="obj.loadtexts[obj.loadstatus]" @clickLoadMore="getMore" /></view>
		<view v-if="!obj.list.length" class="nodata">暂无历史会话</view>
	</view>
</template>

<script setup>
    import { reactive, getCurrentInstance,onMounted } from 'vue'
	import { onReachBottom } from '@dcloudio/uni-app';
	import dayjs from 'dayjs'
    let { proxy } = getCurrentInstance()
	
	let obj = reactive({
		req: {
		  "currentpage": 1,
		  "pagesize": 30
		},
		list: [],
		protoList: [],
		loadstatus: 'more',//more loading noMore
		loadtexts: {
			'more': '查看更多',
			'loading': '加载中',
			'noMore': '没有更多',
		}
	})
	
	onMounted(()=>{
		init()
	})
	
	onReachBottom(()=>{
		getMore()
	})
	
	const init=async()=>{
		obj.req.currentpage = 1
		obj.list = []
		let msg = await getMsg()
		if(msg&&msg.list){
			obj.loadstatus = (obj.req.currentpage * obj.req.pagesize < msg.count)?'more':'noMore';
			setdatas(msg.list||[])
		}
	}
	
	const getMore=async()=>{
		obj.loadstatus = 'loading'
		let msg = await getMsg()
		if(msg&&msg.list){
			obj.loadstatus = (obj.req.currentpage * obj.req.pagesize < msg.count)?'more':'noMore';
			setdatas(msg.list||[])
		}else{
			obj.loadstatus = 'noMore'
		}
	}
	
	const getMsg=()=>{
		return new Promise((resovle,rej)=>{
			proxy.$reQuest.post('mp/user/chat/list',obj.req,{token: true},).then(res=>{
				if(res.success){
					resovle(res.msg)
				}else{resovle(null)}
			}).catch(err=>{resovle(null)})
		})
	}
	
	const setdatas=(list)=>{
		let arr = []
		list.map((e,n)=>{
			if(e.messages&&typeof e.messages == 'object'){
				let time = dayjs().format('YYYY-MM-DD')==e.intime.slice(0,10)?'今天':e.intime.slice(0,10)
				let i = arr.findIndex(ev=>ev&&ev.time==time)
				let bef = arr[i]&&arr[i].arr&&arr[i].arr.length?arr[i].arr[arr[i].arr.length-1]:null//上一条数据
				if(!bef||(bef&&e.type!==bef.type)||(bef&&e.type===bef.type&&e.messages[0].content!==bef.messages[0].content)){//对比上一条数据 如果问题一样就忽略且类别一样
					if(i>-1){
						arr[i].arr.push(e)
					}else{
						arr.push({
							time: time,
							arr: [e],
						})
					}	
				}
			}
		})
		obj.list = obj.list.concat(arr)
		obj.protoList = obj.protoList.concat(list)
		obj.req.currentpage += 1;
	}
	
	const go=(k)=>{
		// 去历史对话
		proxy.$c.go_url(`/pages/index/index?chat_id=${k.chat_id}`,'reLaunch')
		// 去历史详情
		// proxy.$c.go_url(`/pages/chartDetail?id=${k.chat_id}`)
	}
</script>

<style lang="scss" scoped>
.pagebox{
	.timelabel{font-size: 30rpx;font-weight: bold;margin-bottom: 20rpx;}
	.card{
		width: 100%;height: auto;background: #FFFFFF;box-shadow: 0rpx 4rpx 16rpx 0rpx #DAE3EF;border-radius: 25rpx;margin-bottom: 60rpx;
		padding: 10rpx 30rpx;box-sizing: border-box;
		.miniitem{
			.q{font-size: 26rpx;color: #333;}
			.time{font-size: 24rpx;color: #999;}
			padding: 30rpx 0;border-bottom: solid 1rpx #f0f0f0;box-sizing: border-box;
		}
		.miniitem:last-child{border-bottom: none;}
	}
	.nodata{width: 100vw;height: 100vh;text-align: center;line-height: 100vh;font-size: 22rpx;color: #999;}
}
</style>
